﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Collections.ObjectModel;
using Habi.Solver.Models.Resources;

namespace Habi.Solver.Views.Resources
{
    public partial class ResourceTaskControl : XtraScrollableControl
    {


        public ResourceTaskControl() : base()
        {
            Tasks = new ObservableCollection<ResourceTaskModel>();
            Tasks.CollectionChanged += (sender, e) =>
              {
                  InitializeChild();
              };

            Padding = new Padding(6);
        }

        [Browsable(true)]
        public int Columns { get; set; }

        protected override void OnControlAdded(ControlEventArgs e)
        {
            base.OnControlAdded(e);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Reload();
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }

        public override void Refresh()
        {
            base.Refresh();

            Reload();
        }


        private void Reload()
        {
            int scrollBarHeight = Overflow() ? 16 : 0;

            foreach (Control item in Controls) item.Width = (Width - scrollBarHeight);
        }

        private void InitializeChild()
        {
           
            if (Tasks == null || !Tasks.Any()) return;

            int margin = 10;
            foreach (var task in Tasks)
            {
                var item = new ResourceTaskItemControl(task);
                item.Size = new Size(200, 90);

                var childs = Controls.Find(item.Name, false);

                if (childs == null || childs.Length == 0)
                {
                    int top = ContentHeight();
                    item.Location = new Point(0, top + margin);
                    item.Width = Width;
                    item.Visible = true;
                    item.BackColor = Color.FromArgb(255, 232, 232, 232);

                    Controls.Add(item);
                }
            }
        }

        private int ContentHeight()
        {
            if (Controls.Count == 0) return 0;
            var last = Controls[Controls.Count - 1];
            return last.Location.Y + last.Height;
        }

        private bool Overflow()
        {
            int height = Height;
            int sum = 0;
            foreach (Control item in Controls) sum += item.Height;

            return sum > height;
        }
        
        public ObservableCollection<ResourceTaskModel> Tasks { get; set; }
        public IEnumerable<ResourceTaskItemControl> Items
        {
            get
            {
                List<ResourceTaskItemControl> ctrs = new List<ResourceTaskItemControl>();
                foreach (var item in Controls)
                {
                    ctrs.Add(item as ResourceTaskItemControl);
                }

                return ctrs;
            }
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // ResourceTaskControl
            // 
            this.Appearance.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(222)))), ((int)(((byte)(222)))), ((int)(((byte)(222)))));
            this.Appearance.Options.UseBackColor = true;
            this.ResumeLayout(false);

        }
    }
}
